import { defineStore } from 'pinia'
import { UserState } from '../type'
import { getToken, removeToken } from '@/utils/token'
import { logout } from '@/apis/user'
import { ElMessage } from 'element-plus'
const useUserStore = defineStore("useUserStore",{
    state:():UserState =>({
        id: undefined,
        avatar:"",
        nicename:"",
        username:"",
        permissions:[],
        isLogin:false,
    }),
    actions:{
        // Login(){
        //     return new Promise((resolve,reject)=>{

        //     })
        // },
        Logout(){
            return new Promise((resolve,reject)=>{
                logout().then((res)=>{
                    resolve(res);
                    ElMessage.success("退出成功");
                })
                this.$reset();
                removeToken();
                this.isLogin = false;
                resolve(null);
            })
        },
        forceLogout(){
            this.$reset();
            removeToken();
            this.isLogin = false;
        },
        getPerm(){
            return this.permissions;
        },
        setPerm(perm:string[]){
            this.permissions = perm;
        }
    },
    persist:{
        key: "user",
        storage: sessionStorage,
    }
})
export default useUserStore;